package com.hina.rec.application.service.system.impl;

import cn.dev33.satoken.stp.StpInterface;
import com.hina.rec.application.manager.system.MenuManager;
import com.hina.rec.application.manager.system.RoleManager;
import com.hina.rec.domain.model.MenuDO;
import com.hina.rec.domain.model.RoleDO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义权限加载接口实现类
 *
 * @author liaosp
 */
//@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {

    private final RoleManager roleManager;
    private final MenuManager menuManager;


    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        //
        // 获取用户角色列表
        List<RoleDO> roles = roleManager.getRolesByUserId(Integer.valueOf(String.valueOf(loginId)));
        // 获取用户菜单列表
        List<MenuDO> menus = menuManager.getMenusByRoleIds(roles.stream().map(RoleDO::getId).toList());
        // 获取用户权限列表
        return menus.stream().map(MenuDO::getPermission).toList();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<RoleDO> rolesByUserId = roleManager.getRolesByUserId(Integer.valueOf(String.valueOf(loginId)));
        return rolesByUserId.stream().map(RoleDO::getRoleCode).toList();
    }
}
